/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * janelaCliente.java
 *
 * Created on 26/05/2009, 09:59:45
 */
package gui;

import java.rmi.RemoteException;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;
import rmiteste.Cliente;
import rmiteste.atualizar;

/**
 *
 * @author fosa
 */
public class janelaCliente extends javax.swing.JFrame {

    /** Creates new form janelaCliente */
    public janelaCliente() {
        initComponents();
        janelalogin = new Login();
        atualizar cm = new atualizar();
        timer = new Timer();
        timer.schedule(cm, 0, 1 * 2000);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mensageiro");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Vai!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextArea2.setColumns(20);
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(1);
        jTextArea2.setWrapStyleWord(true);
        jTextArea2.setDragEnabled(true);
        jTextArea2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextArea2KeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(jTextArea2);

        jScrollPane3.setViewportView(jList1);

        jMenu1.setText("Arquivo");

        jMenuItem1.setText("Conectar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Sair");
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Editar");

        jMenuItem3.setText("Atualizar");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        janelalogin.show();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jTextArea2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea2KeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
            if (getJList1().isSelectionEmpty()) {
                try {
                    Cliente.getM().enviarMensagem(jTextArea2.getText(), Cliente.getMeuid());
                    jTextArea2.setText(null);
                } catch (RemoteException ex) {
                    Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                int[] selecionados = getJList1().getSelectedIndices();
                try {
                    for (int i = 0; i < selecionados.length; i++) {
                        Cliente.getM().enviarMensagem(selecionados[i], jTextArea2.getText(), Cliente.getMeuid());
                    }
                    Cliente.getM().enviarMensagem(Cliente.getMeuid(), jTextArea2.getText(), Cliente.getMeuid());

                    jTextArea2.setText(null);
                } catch (RemoteException ex) {
                    Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NullPointerException ex) {
                    Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_jTextArea2KeyPressed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        try {
            // TODO add your handling code here:


            String[] mensagens = Cliente.getM().lerMensagens(Cliente.getMeuid());

            for (int i = 0; i < mensagens.length; i++) {
                addtexto(mensagens[i]);
            }

            setmodel(Cliente.getM().uruarios());
        } catch (RemoteException ex) {
            Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (getJList1().isSelectionEmpty()) {
            try {
                Cliente.getM().enviarMensagem(jTextArea2.getText(), Cliente.getMeuid());
                jTextArea2.setText(null);
            } catch (RemoteException ex) {
                Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            int[] selecionados = getJList1().getSelectedIndices();
            try {
                for (int i = 0; i < selecionados.length; i++) {
                    Cliente.getM().enviarMensagem(selecionados[i], jTextArea2.getText(), Cliente.getMeuid());
                }
                Cliente.getM().enviarMensagem(Cliente.getMeuid(), jTextArea2.getText(), Cliente.getMeuid());

                jTextArea2.setText(null);
            } catch (RemoteException ex) {
                Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NullPointerException ex) {
                Logger.getLogger(janelaCliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new janelaCliente().setVisible(true);
            }
        });
    }
    Timer timer;
    Login janelalogin;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the jTextArea1
     */
    public javax.swing.JTextArea getJTextArea1() {
        return jTextArea1;
    }

    /**
     * @param jTextArea1 the jTextArea1 to set
     */
    public void addtexto(String _texto) {
        jTextArea1.setText(jTextArea1.getText() + "\n" + _texto);
    }

    public void setmodel(final String[] str) {
        getJList1().setModel(new javax.swing.AbstractListModel() {

            String[] strings = str;

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(
                    int i) {
                return strings[i];
            }
        });
    }

    /**
     * @return the jList1
     */
    public javax.swing.JList getJList1() {
        return jList1;
    }

    /**
     * @param jList1 the jList1 to set
     */
    public void setJList1(javax.swing.JList jList1) {
        this.jList1 = jList1;
    }
}
